package com.qf.controller;

import com.qf.aop.annocation.LoginUser;
import com.qf.entity.*;
import com.qf.feign.api.CarService;
import com.qf.feign.api.IAddressService;
import com.qf.service.IOrderService;
import com.qf.utils.OrderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/orderController")
public class OrderController {

    @Autowired
    private CarService carService;

    @Autowired
    private IAddressService addressService;

    @Autowired
    private OrderUtils orderUtils;

    @Autowired
    private IOrderService orderService;

    @RequestMapping("/toOrderConfirmPage")
    @LoginUser
    public String toOrderConfirmPage(User user, Model model) {

        // 1.判断用户是否登录
        if (user == null) {
            return "redirect:http://localhost/shop-sso/toLoginPage";
        }

        // 2.根据用户Id查询地址
        List<Address> addressListByUserId = addressService.getAddressListByUserId(user.getId());


        // 3.根据用户id查询购物车
        List<Car> carList = carService.getCarListByUserId(user.getId());

        BigDecimal totalPrice = carTotalPrice(carList);

        // 4.把数据放入到Model中
        model.addAttribute("carList", carList);
        model.addAttribute("addressList", addressListByUserId);
        model.addAttribute("totalPrice", totalPrice);

        // 5.设置跳转的视图
        return "orderConfirm";
    }

    public BigDecimal carTotalPrice(List<Car> carList) {

        // 计算总价
        BigDecimal totalPrice = new BigDecimal(0.0);

        for (Car car : carList) {
            BigDecimal gprice = car.getGoods().getGprice();
            Integer count = car.getCount();
            // 单价乘以数量
            totalPrice = totalPrice.add(gprice.multiply(BigDecimal.valueOf(count)));
        }
        return totalPrice;
    }

    @RequestMapping("/createOrder")
    @LoginUser
    public String createOrder(User user, Integer addressId) {

        // 1.判断用户是否登录
        if (user == null) {
            return "redirect:http://localhost/shop-sso/toLoginPage";
        }

        // 2.根据地址Id查询地址的信息
        Address address = addressService.getAddressById(addressId);

        // 3.查询购物车信息
        List<Car> carList = carService.getCarListByUserId(user.getId());

        // 4.计算购车的总价
        BigDecimal totalPrice = carTotalPrice(carList);

        String orderId = orderUtils.createOrderId(user.getId());


        // 5.封装Order对象
        Order order = new Order();
        order.setId(orderId); // 自己生成
        order.setPhone(address.getPhone());
        order.setAddress(address.getAddress());
        order.setUsername(address.getUsername());
        order.setStatus(1); // 1: 未支付
        order.setCreateTime(new Date());
        order.setUid(user.getId());
        order.setTotalPrice(totalPrice);

        // 6.插入订单表 --> 2数据库
        orderService.addOrder(user, order, carList);

        // 7.清空购物车
//        carService.clearCar(user.getId());

        // 8.跳转到支付页面
        return "redirect:http://localhost/shop-pay/ali/pay?oid=" + orderId;
    }

    @RequestMapping("/getOrderById/{oid}")
    @ResponseBody
    public Order getOrderById(@PathVariable String oid) {
        // 1、根据oid解析出userId后四位
        Integer userIdSuf = orderUtils.getUserSufIdByOrderId(oid);
        return orderService.getOrderById(oid, userIdSuf);
    }

    @RequestMapping("/updateOrderStatus")
    public void updateOrderStatus(@RequestParam("oid") String oid, @RequestParam("status") Integer status) {
        Integer userIdSuf = orderUtils.getUserSufIdByOrderId(oid);
        orderService.updateOrderStatus(userIdSuf,oid,status);
    }
}
